package com.fs.service.impl;

import cn.hutool.core.util.StrUtil;
import com.fs.constant.CacheConstants;
import com.fs.constant.UserConstants;
import com.fs.controller.CaptchaController;
import com.fs.entity.Admins;
import com.fs.service.AdminsService;
import com.fs.service.LoginService;
import com.fs.util.RedisCache;
import com.fs.util.ResponseData;
import com.fs.util.ResponseDataUtil;
import com.fs.util.ResultEnums;
import com.fs.vo.LoginAdmins;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author hua
 * @version 1.0
 * @title AdminsServiceImpl
 * @description 登录服务实现类
 * @create 2024/4/17 16:33
 */
@Service("LoginService")
@Slf4j
public class loginServiceImpl implements LoginService {
    @Autowired
    private TokenService tokenService;


    @Autowired
    private RedisCache redisCache;

    @Autowired
    private AdminsService adminsService;

    @Autowired
    private PasswordService passwordService;

    @Autowired
    private CaptchaController captchaController;
    /**
     * 登录验证
     *
     * @param username 用户名
     * @param password 密码
     * @param code 验证码
     * @param uuid 唯一标识
     * @return 结果
     */
    public ResponseData login(String username,String password,String code,String uuid) {

//        uuid=captchaController.captchaImage().getMeta().getMsg();
//        code=redisCache.getCacheObject(CacheConstants.CAPTCHA_CODE_KEY+uuid);

        // 【1】验证码校验
        ResponseData responseData = validateCaptcha(username, code, uuid);
        if (responseData.getMeta().getStatus()==400){
            return responseData;
        }
        // 【2】登录前置校验
        responseData = loginPreCheck(username, password);
        if (responseData.getMeta().getStatus()==400){
            return responseData;
        }
        // 【3】用户验证，验证成功获取用户对象
        Admins admins = adminsService.selectAdminsByUserName(username);
        if (Objects.isNull(admins)) {
            log.info("登录管理员：{} 不存在.", username);
            return ResponseDataUtil.buildError(400,"管理员不存在");
        }
        //【4】.校验用户密码
        responseData = passwordService.validate(admins, password);
        if (responseData.getMeta().getStatus()==400){
            return responseData;
        }
        LoginAdmins loginAdmins = new LoginAdmins();
        loginAdmins.setPwd(password);
        loginAdmins.setUsername(username);
        loginAdmins.setAdmins(admins);
        // 【5】生成token数据
        String token = tokenService.createToken(loginAdmins);
        loginAdmins.setToken(token);
        return ResponseDataUtil.buildOk(loginAdmins);
    }

    /**
     * 校验验证码
     *
     * @param username 用户名
     * @param code     验证码
     * @param uuid     唯一标识
     * @return 结果
     */
    public ResponseData validateCaptcha(String username, String code, String uuid) {
        String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + uuid;
        String captcha = redisCache.getCacheObject(verifyKey);
        redisCache.deleteObject(verifyKey);
        if (captcha == null) {
            return ResponseDataUtil.buildError(400,"验证码不能为空");
        }
        if (!code.equalsIgnoreCase(captcha)) {
            return ResponseDataUtil.buildError(400,"验证码错误");
        }
        return ResponseDataUtil.buildOk(ResultEnums.OK);
    }

    /**
     * 登录前置校验
     *
     * @param username 用户名
     * @param password 用户密码
     */
    public ResponseData loginPreCheck(String username, String password) {
        // 用户名或密码为空 错误
        if (StrUtil.isEmpty(username) || StrUtil.isEmpty(password)) {
            return ResponseDataUtil.buildError(400,"用户名或密码为空");
        }
        // 密码如果不在指定范围内 错误
        if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
                || password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
            return ResponseDataUtil.buildError(400,"密码如果不在指定范围内");
        }
        // 用户名不在指定范围内 错误
        if (username.length() < UserConstants.USERNAME_MIN_LENGTH
                || username.length() > UserConstants.USERNAME_MAX_LENGTH) {
            return ResponseDataUtil.buildError(400,"用户名不在指定范围内");
        }
        return ResponseDataUtil.buildOk(ResultEnums.OK);
    }

}
